package com.stoneframe.util.baseJson;

import java.util.ArrayList;
import java.util.List;

import com.stoneframe.entity.other.TreeEntity;
import com.stoneframe.entity.other.TreeGridEntity;
import com.stoneframe.util.baseApp.PageData;
import com.stoneframe.util.baseApp.ToolHelper;

public class TreeJsonHelper {
	
    public static String treeToJson(List<TreeEntity> list, String parentId){
        StringBuilder strJson = new StringBuilder();
        
        List<TreeEntity> item =new ArrayList<TreeEntity>();
        for(TreeEntity treeEntity:list){
        	if(treeEntity.getParentId().equals(parentId)){
        		item.add(treeEntity);
        	}
        }
        strJson.append("[");
        if (item.size() > 0){
            for(TreeEntity entity:item){
                strJson.append("{");
                strJson.append("\"id\":\"" + entity.getId() + "\",");
                strJson.append("\"text\":\"" + entity.getText().replace("&nbsp;", "") + "\",");
                strJson.append("\"value\":\"" + entity.getValue() + "\",");
                if (ToolHelper.notEmpty(entity.getAttribute())){
                    strJson.append("\"" + entity.getAttribute() + "\":\"" + entity.getAttributeValue() + "\",");
                }
                if (ToolHelper.notEmpty(entity.getAttributeA())){
                    strJson.append("\"" + entity.getAttributeA() + "\":\"" + entity.getAttributeValueA() + "\",");
                }
                if (ToolHelper.notEmpty(entity.getTitle())){
                    strJson.append("\"title\":\"" + entity.getTitle().replace("&nbsp;", "") + "\",");
                }
                if (ToolHelper.notEmpty(entity.getImg())){
                    strJson.append("\"img\":\"" + entity.getImg().replace("&nbsp;", "") + "\",");
                }
                if (entity.getCheckState()!=0){
                    strJson.append("\"checkstate\":" + entity.getCheckState() + ",");
                }
                if (ToolHelper.notEmpty(entity.getParentId())){
                    strJson.append("\"parentnodes\":\"" + entity.getParentId() + "\",");
                }
                if (entity.getLevel() != -1){
                    strJson.append("\"Level\":" + entity.getLevel() + ",");
                }
                strJson.append("\"showcheck\":" + entity.getShowCheck() + ",");
                strJson.append("\"isexpand\":" + entity.getIsexpand() + ",");
                if (entity.getComplete()){
                    strJson.append("\"complete\":" + entity.getComplete() + ",");
                }
                strJson.append("\"hasChildren\":" + entity.getHasChildren() + ",");
                strJson.append("\"ChildNodes\":" + treeToJson(list, entity.getId()) + "");
                strJson.append("},");
            }
            strJson = strJson.delete(strJson.length() - 1, strJson.length());
        }
        strJson.append("]");
        return strJson.toString();
    }
    
    /**
     * 转换Grid树形Json
     * */
    public static String treeGridToJson(List<TreeGridEntity> list){
    	StringBuilder sb = new StringBuilder();
        sb.append("{ \"rows\": [");
        sb.append(treeGridJson(list, -1, "0"));
        sb.append("]}");
        return sb.toString();
    }
    
    public static int lft = 1, rgt = 1000000;
    public static String treeGridJson(List<TreeGridEntity> list, int index, String parentId){
        StringBuilder sb = new StringBuilder();
        
        List<TreeGridEntity> subItem =new ArrayList<TreeGridEntity>();
        for(TreeGridEntity treeGridEntity:list){
        	if(treeGridEntity.getParentId().equals(parentId)){
        		subItem.add(treeGridEntity);
        	}
        }
        
        if (subItem.size() > 0) { 
        	index++; 
        }
        
        for (TreeGridEntity entity : subItem){
        	StringBuilder strJson = new StringBuilder(entity.getEntityJson());
            strJson = strJson.insert(1, "\"level\":" + index + ",");
            strJson = strJson.insert(1, "\"isLeaf\":" + (!entity.getHasChildren()) + ",");
            strJson = strJson.insert(1, "\"expanded\":" + entity.getExpanded().toString().toLowerCase() + ",");
            strJson = strJson.insert(1, "\"lft\":" + lft++ + ",");
            strJson = strJson.insert(1, "\"rgt\":" + rgt-- + ",");
            sb.append(strJson.toString());
            sb.append(treeGridJson(list, index, entity.getId()));
        }
        return sb.toString().replace("}{", "},{");
    }

}
